<?php
/**
 * 《后台管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Admin.class.php 809 2009-12-31 09:20:37Z sunxy $s
 */

require_once(dirname(__FILE__) . '/AdminUtil.class.php');

class Admin extends AdminUtil {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:08 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function login($controller, $request) {

		$controller->assign('postLink', "?do=Admin.loginPost");
		$controller->display($request, 'login');
		return true;
	}

	/**
	 * 登录
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function loginPost($controller, $request) {
		$username = trim($request['username']);
		$password = trim($request['password']);
		
		// require_once(dirname(__FILE__) . '/emp/Emp.class.php');
		// $emp = new Emp();
		
		if ($row = _emp($username)) {
		// if ($row = $emp->_by_username($username)) {				
			if (!_password($row['password'], $password)) {
				debug("passord error.");
				$controller->display($request, 'login');
				return false;
			}
				
			$_SESSION['emp'] = $row;
			$_SESSION['id'] = $row['id'];
			$_SESSION['code'] = $row['code'];
			$_SESSION['office'] = $row['office'];
			$_SESSION['dept'] = $row['dept'];
			$_SESSION['name'] = $row['name'];
			$_SESSION['admin'] = $row['admin'];
			
			if ($row['state'] < 3) {
				header("Location: resume.php");
				return true;
			}

			// $this->action_my($row['id']);
			require_once(dirname(__FILE__) . '/emp/EmpRole.class.php');
			$aa = new EmpRole();
			$ret = $aa->action_my($row['id']);
			$this->tree= $ret;
			
			// debug(print_r($ret, true));
			
			$_SESSION['mytree'] = $this->tree;
		} else {
			$controller->display($request, 'login');
			return false;
		}

		header("Location: index.php");
		return true;
	}
	
	function ajax_exist(&$controller, &$request) {
		$username = trim($request['username']);
		$password = trim($request['password']);
	
		if ($row = _emp($username)) {
			$row['password'] = _password($row['password'], $password);
	
			$ret['data'] = $row;
		} else {
			$ret['code'] = 0;
		}
	
		echo json_encode($ret);
	}

	var $tree = array();

	function action_my($emp) {
		$db = newdb(Constant::tbl_emp_role);
		$fmt = "SELECT * FROM %s WHERE emp = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_role, $emp);
		// debug($sql);
		$result = $db->query($sql);
		while($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				$this->action_role($row['role']);
			}
		}
		
		// $this->action_role(11);	// 缺省，公共组

		$this->action_emp($emp);
	}

	function action_role($role) {
		$db = newdb(Constant::tbl_role_priv);
		$fmt = "SELECT * FROM %s WHERE role = '%s'";
		$sql = sprintf($fmt, Constant::tbl_role_priv, $role);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				$aa = explode('.', $row['action']);
				$s = $row['cc'] . "?do=" . $row['action'];
				$this->tree[$s] = $s;
			}
		}

		return true;
	}

	function action_emp($emp) {
		$db = newdb(Constant::tbl_emp_priv);
		$fmt = "SELECT * FROM %s WHERE emp = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_priv, $emp);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				$aa = explode('.', $row['action']);
				$s = $row['cc'] . "?do=" . $aa[0] . ".";
				// $s = $row['cc'] . "?do=" . $aa[0];
				$this->tree[$s] = $s;
			}
		}

		return true;
	}

	/**
	 * 注销会话
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function logout($controller, $request) {
		@session_start();
		$_SESSION = array();

		$controller->assign('emp', '');

		header("Location: index.php");
		return true;
	}
}
?>